﻿namespace Lzhdim.WinSrvMgr.Forms
{
    using Lzhdim.Animation.UI;
    using Lzhdim.MenuHelper;
    using Lzhdim.WinSrvMgr.UI;
    using System;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;

    public partial class WndMain : FormBase
    {
        private string serviceFilePath = string.Empty;
        private string serviceName = string.Empty;

        public WndMain()
        {
            InitializeComponent();

            this.Icon = Properties.Resources.Control_Panel1;
            this.tsmAppName.Image = Properties.Resources.Control_Panel;

            this.Size = this.MaximumSize = this.MinimumSize = new Size(this.PanelBackground.Width, this.PanelBackground.Height);

            AddRightMenu(this, Constant.WM_UserAboutApp, "关于此应用(&A)...");
        }

        #region 添加并处理系统菜单

        /// <summary>
        /// 往系统菜单里添加项
        /// </summary>
        /// <param name="form">窗体</param>
        /// <param name="wIDNewItem">菜单ID</param>
        /// <param name="lpNewItem">菜单项内容</param>
        internal void AddRightMenu(Form form, Int32 wIDNewItem, string lpNewItem)
        {
            IntPtr hMenu = SystemMenu.GetSystemMenu(form.Handle, false);
            SystemMenu.InsertMenu(hMenu, 5, SystemMenu.MF_BYPOSITION | SystemMenu.MF_SEPARATOR, 0, string.Empty);
            SystemMenu.InsertMenu(hMenu, 6, SystemMenu.MF_BYPOSITION, wIDNewItem, lpNewItem);
            //DeleteMenu(hMenu, 1, MF_BYPOSITION);    //2024年3月5日添加删除移动系统菜单项
        }

        /// <summary>
        /// 处理系统消息，这里用于处理系统菜单里的操作
        /// </summary>
        /// <param name="m">消息代码</param>
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == SystemMenu.WM_SYSCOMMAND)
            {
                switch (m.WParam.ToInt32())
                {
                    //处理显示关于窗体操作
                    case Constant.WM_UserAboutApp:
                        if (Windows.Instance.AboutForm == null)
                        {
                            Windows.Instance.AboutForm = new WndAbout();

                            if (Windows.Instance.AboutForm.ShowDialog(this) == DialogResult.OK)
                            {
                                Windows.Instance.AboutForm = null;
                            }
                        }
                        break;
                }
            }

            base.WndProc(ref m);
        }

        #endregion 添加并处理系统菜单

        private void btnInstallSrv_Click(object sender, EventArgs e)
        {
            this.btnSelFile.Enabled = false;
            this.btnInstallSrv.Enabled = false;
            this.btnStartSrv.Enabled = false;
            this.btnStopSrv.Enabled = false;
            this.btnUnInstallSrv.Enabled = false;

            if (serviceName != string.Empty)
            {
                try
                {
                    if (ServiceHelper.IsServiceExisted(serviceName)) ServiceHelper.UninstallService(serviceFilePath);
                    ServiceHelper.InstallService(serviceFilePath);

                    MessageBox.Show("服务安装成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            this.btnSelFile.Enabled = true;
            this.btnInstallSrv.Enabled = true;
            this.btnStartSrv.Enabled = true;
            this.btnStopSrv.Enabled = true;
            this.btnUnInstallSrv.Enabled = true;
        }

        private void btnSelFile_Click(object sender, EventArgs e)
        {
            this.btnSelFile.Enabled = false;
            this.btnInstallSrv.Enabled = false;
            this.btnStartSrv.Enabled = false;
            this.btnStopSrv.Enabled = false;
            this.btnUnInstallSrv.Enabled = false;

            OpenFileDialog dialog = new OpenFileDialog
            {
                InitialDirectory = Application.StartupPath,
                Filter = "服务程序|*.exe",
                RestoreDirectory = true,
                FilterIndex = 1
            };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.txbSrvFile.Text = dialog.FileName;
                serviceFilePath = dialog.FileName;
                serviceName = Path.GetFileNameWithoutExtension(serviceFilePath);
            }

            this.btnSelFile.Enabled = true;
            this.btnInstallSrv.Enabled = true;
            this.btnStartSrv.Enabled = true;
            this.btnStopSrv.Enabled = true;
            this.btnUnInstallSrv.Enabled = true;
        }

        private void btnStartSrv_Click(object sender, EventArgs e)
        {
            this.btnSelFile.Enabled = false;
            this.btnInstallSrv.Enabled = false;
            this.btnStartSrv.Enabled = false;
            this.btnStopSrv.Enabled = false;
            this.btnUnInstallSrv.Enabled = false;

            if (serviceName != string.Empty)
            {
                try
                {
                    if (ServiceHelper.IsServiceExisted(serviceName)) ServiceHelper.ServiceStart(serviceName);

                    MessageBox.Show("服务启动成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            this.btnSelFile.Enabled = true;
            this.btnInstallSrv.Enabled = true;
            this.btnStartSrv.Enabled = true;
            this.btnStopSrv.Enabled = true;
            this.btnUnInstallSrv.Enabled = true;
        }

        private void btnStopSrv_Click(object sender, EventArgs e)
        {
            this.btnSelFile.Enabled = false;
            this.btnInstallSrv.Enabled = false;
            this.btnStartSrv.Enabled = false;
            this.btnStopSrv.Enabled = false;
            this.btnUnInstallSrv.Enabled = false;

            if (serviceName != string.Empty)
            {
                try
                {
                    if (ServiceHelper.IsServiceExisted(serviceName)) ServiceHelper.ServiceStop(serviceName);

                    MessageBox.Show("服务停止成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            this.btnSelFile.Enabled = true;
            this.btnInstallSrv.Enabled = true;
            this.btnStartSrv.Enabled = true;
            this.btnStopSrv.Enabled = true;
            this.btnUnInstallSrv.Enabled = true;
        }

        private void btnUnInstallSrv_Click(object sender, EventArgs e)
        {
            this.btnSelFile.Enabled = false;
            this.btnInstallSrv.Enabled = false;
            this.btnStartSrv.Enabled = false;
            this.btnStopSrv.Enabled = false;
            this.btnUnInstallSrv.Enabled = false;

            if (serviceName != string.Empty)
            {
                try
                {
                    if (ServiceHelper.IsServiceExisted(serviceName))
                    {
                        ServiceHelper.ServiceStop(serviceName);
                        ServiceHelper.UninstallService(serviceFilePath);

                        MessageBox.Show("服务卸载成功");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            this.btnSelFile.Enabled = true;
            this.btnInstallSrv.Enabled = true;
            this.btnStartSrv.Enabled = true;
            this.btnStopSrv.Enabled = true;
            this.btnUnInstallSrv.Enabled = true;
        }

        /// <summary>
        /// 移动条鼠标按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lblMoveBar_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                MouseDownEffect(this);
            }
        }

        /// <summary>
        /// 移动条鼠标弹起事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lblMoveBar_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ShowSystemMenu(e.Button);
            }
        }

        private void TSMAbout_Click(object sender, EventArgs e)
        {
            if (Windows.Instance.AboutForm == null)
            {
                Windows.Instance.AboutForm = new WndAbout();

                if (Windows.Instance.AboutForm.ShowDialog(this) == DialogResult.OK)
                {
                    Windows.Instance.AboutForm = null;
                }
            }
        }

        private void tsmClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}